package com.dayaokeji.rhythmschoolstudent.wiget

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AppCompatDialogFragment
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import com.dayaokeji.rhythmschoolstudent.R

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/19
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 ** 创建群组
 *********************************************************************
 */
class CreateLeaveDialog : AppCompatDialogFragment(), View.OnClickListener {

    companion object {
        fun newInstance(): CreateLeaveDialog = CreateLeaveDialog()
    }

    private lateinit var etLeaveReason: EditText

    private var submitListener: SubmitListener? = null
    private var addImageClickListener: AddImageClickListener? = null
    private var leaveImageLongClickListener: LeaveImageLongClickListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setStyle(DialogFragment.STYLE_NO_FRAME,0)
        val rootView = inflater.inflate(R.layout.dialog_create_leave, container, false)
        rootView.minimumWidth = activity?.resources?.displayMetrics?.widthPixels!!

        etLeaveReason = rootView.findViewById(R.id.et_leave_reason)
        val btnCreateGroup = rootView.findViewById<Button>(R.id.btn_submit)
        btnCreateGroup.setOnClickListener(this)
        val ivAddImage = rootView.findViewById<ImageView>(R.id.iv_add_img)
        ivAddImage.setOnClickListener(this)
        ivAddImage.setOnLongClickListener{
            it?.let {
                leaveImageLongClickListener?.longClick(ivAddImage)
            }
            true
        }
        return rootView
    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.btn_submit -> {
                    val groupIntro = etLeaveReason.text.toString()
                    val leaveInfo = LeaveInfo(groupIntro)
                    if (submitListener != null) {
                        submitListener?.click(leaveInfo)
                    }
                }
                R.id.iv_add_img -> {
                    if (addImageClickListener != null){
                        if (v is ImageView) {
                            addImageClickListener?.click(v)
                        }
                    }
                }
            }
        }
    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        val attributes = dialog.window.attributes
        attributes.gravity = Gravity.BOTTOM
        attributes.windowAnimations = R.style.BottomDialogAnimation
        dialog.setCanceledOnTouchOutside(true)
        dialog.onWindowAttributesChanged(attributes)
        return dialog
    }


    fun setSubmitListener(submitListener: SubmitListener){
        this.submitListener = submitListener
    }

    fun setAddImageClickListener(addImageClickListener: AddImageClickListener) {
        this.addImageClickListener = addImageClickListener
    }

    fun setLeaveImageLongClickListener(leaveImageLongClickListener: LeaveImageLongClickListener) {
        this.leaveImageLongClickListener = leaveImageLongClickListener
    }

    interface SubmitListener{
        fun click(leaveInfo: LeaveInfo)
    }

    interface AddImageClickListener {
        fun click(view: ImageView)
    }

    interface LeaveImageLongClickListener{
        fun longClick(view: ImageView)
    }
}

data class LeaveInfo(val reason: String)